#! /bin/bash
# DWM自启动脚本

# 启动时查看是否已经启动，防止重复启动
# 注：此种方法是模糊搜索 如：搜索fcitx时 如果有fcitx5会认为有
function run_no_restart_app(){
    echo "[INFO] --- run_no_restart_app ---"
    searchWord=$1
    echo "[INFO] run_no_restart_app "$searchWord
    # 获取全部的相关的程序信息
    # ps -aux|grep "$searchWord" | grep -v grep
    # 获取全部的相关程序的进程号
    pids=$(ps -ef | grep "$searchWord" | grep -v grep  | awk '{print $2}')
    # 注：此种方法是模糊搜索
    if [ $pids ]; then
        # 有相关进程，则不做其他操作
        # echo "[INFO] 已启动"$searchWord
        echo "[INFO] 模糊查询匹配查到关键字"$searchWord"程序进程"
    else
        # 无相关进程，则启动执行启动
        # 判断是否有第二个参数，如果有则把第二个参数当作执行命令，没有则把第一个参数当执行命令
        if [ "$2" ];then
            echo "[INFO] 有第二个参数"
            startappCmd=$2
            echo "[Debug] param2="${startappCmd};
        else
            startappCmd=$1
        fi
        # 建立软链接命令拼接： cmdStr="ln -s  "${dir}"/scripts "${HOME}"/.dwm"
        # 启动后台运行命令拼接
        cmdStr=$startappCmd
        echo "[INFO] 执行命令: ${cmdStr} 启动相关程序"
        # 后台执行
        ${cmdStr} &
        # ${cmdStr} >> /dev/null 2>&1
    fi
}

# 启动时查看是否已经启动，若已启动则杀死后重启
function restart_app(){
    echo "[INFO] --- restart_app ---"
    searchWord=$1
    echo "[INFO] restart_app "$searchWord
    # 获取全部的相关的程序信息
    # ps -aux|grep "$searchWord" | grep -v grep
    # ps -aux|grep "dwm-status.sh" | grep -v grep

    # 获取全部的相关程序的进程号
    pids=$(ps -ef | grep "$searchWord" | grep -v grep  | awk '{print $2}')
    if [ $pids ]; then
        # 有相关进程，则不做其他操作
        echo "[INFO] 模糊查询匹配查到关键字"$searchWord"程序进程"
        # 判断查询结果
        # 循环一个个关闭
        for pid in $pids
        do
            echo  $pid
            kill -9  $pid
        done
    else
        echo "[INFO] 未启动"$searchWord
    fi

    # 判断是否有第二个参数，如果有则把第二个参数当作执行命令，没有则把第一个参数当执行命令
    if [ "$2" ];then
        echo "[INFO] 有第二个参数"
        startappCmd=$2
        echo "[INFO] startappCmd="${startappCmd};
    else
        startappCmd=$1
    fi

    # 执行启动命令
    # 建立软链接命令拼接： cmdStr="ln -s  "${dir}"/scripts "${HOME}"/.dwm"
    # 启动后台运行命令拼接
    cmdStr=$startappCmd
    echo "[INFO] 执行命令: ${cmdStr} 启动相关程序"
    # 后台执行
    ${cmdStr} &
    # ${cmdStr} >> /dev/null 2>&1
    
}


settings() {
    # 延迟几秒
    [ $1 ] && sleep $1
    
    # xset s 600
    # xset -
    # syndaemon -i 1 -t -K -R -d
    # xss-lock -- ~/.dwm/scripts/app-starter.sh blurlock &
    # 设置屏幕分辨率
    # $HOME/.dwm/scripts/set-screen.sh &

    # 设置壁纸
    # 随机选择文件夹下的一张图片作为壁纸 
    # feh --recursive --randomize --bg-fill ~/Pictures/wallpapers/*.jpg
    bash $HOME/.dwm/scripts/set-wallpaper.sh &

    # 更新一次状态栏
    # $HOME/.dwm/scripts/dwm-status.sh &
    # bash $HOME/.dwm/scripts/dwm-statuscmd-v1.0.sh &
    bash $HOME/.dwm/scripts/dwm-statuscmd-v2.0.sh &

}

daemons() {
    # 延迟几秒
    [ $1 ] && sleep $1

    # 状态栏内容定时自动更新
    restart_app "dwm-status-autoupdate.sh" $HOME"/.dwm/scripts/dwm-status-autoupdate.sh"
    
    # 自动切换壁纸
    # restart_app "set-wallpaper-autochange.sh" $HOME"/.dwm/scripts/set-wallpaper-autochange.sh"

    # 开启渲染器
    # 渲染器
    # picom &
    # picom --experimental-backends --config ~/.dwm/scripts/config/picom.conf >> /dev/null 2>&1 &
    # picom  --config ~/.dwm/scripts/config/picom.conf >> /dev/null 2>&1 &
    # norestart_app "picom" "picom  --config "$HOME"/.dwm/scripts/config/picom.conf >> /dev/null 2>&1";
    # >> /dev/null 2>&1 error
    if [[ -x "$( which picom )" ]]; then
        echo "[INFO] 有 picom"
        restart_app "picom"
        # restart_app "picom" "picom  --config "$HOME"/.dwm/scripts/config/picom.conf "
    else
        echo "[INFO] 无 picom"
    fi

    # 辅助单独设置窗口透明度
    if [[ -x "$( which transset-df  )" ]]; then
        echo "[INFO] 有 transset-df"
        # transset-df  0.6
        restart_app "transset-df" " transset-df  0.6"
    else
        echo "[INFO] 无 transset-df"
    fi
    # 解决在窗口管理器发生的Java GUIs渲染问题
    # 冒充另一个窗口管理器（冒充 LG3D）
    if [[ -x "$( which wmname )" ]]; then
        echo "[INFO] 有 wmname"
        # wmname LG3D &
        run_no_restart_app "wmname LG3D"
    else
        echo "[INFO] 无 wmname "
    fi
    
    # 输入法：
    if [[ -x "$( which fcitx5 )" ]]; then
        echo "[INFO] 有安装 fcitx5"
        # fcitx5 >> /dev/null 2>&1 &
        run_no_restart_app "fcitx5"
        # ${XDG_CONFIG_HOME:-$HOME/.config}/x11/setup.sh
        # fcitx5 &
        # if [ -f "${XDG_CONFIG_HOME:-$HOME/.config}/x11/xprofile" ]; then
        #     . "${XDG_CONFIG_HOME:-$HOME/.config}/x11/xprofile"
        # else
        #     . "$HOME/.xprofile"
        # fi
    elif [[ -x "$( which fcitx )" ]]; then
        echo "[INFO] 有安装 fcitx"
        # fcitx >> /dev/null 2>&1 &
        run_no_restart_app "fcitx"
    else
        echo "[INFO] 无 fcitx fcitx5 "
    fi
    # ~/.dwm/scripts/app-starter.sh picom &
    # 设备信息
    # pactl info &

    # rust的自定义独立小部件,默认配置文件位置是~/.config/eww
    if [[  -x "$( which eww )" ]]; then
        echo "[INFO] 有安装 eww "
        # eww --config ~/.config/eww daemon >> /dev/null 2>&1 &
        # restart_app "eww"  "eww --config "$HOME"/.config/eww daemon "
        restart_app "eww"  "eww --config "$HOME"/.dwm/scripts/config/eww daemon "
    fi

    # 系统监视小部件 conky
    # if [[  -x "$( which conky )" ]]; then
    #     echo "[INFO] 有安装 conky "
    #     # $HOME/.config/conky/Auva/start.sh &
    #     # restart_app "$HOME/.dwm/scripts/config/conky/Auva/start.sh"
    #     restart_app "conky" "conky -c "$HOME"/.dwm/scripts/config/conky/conky.conf"
    # else
    #     echo "[INFO] 无 conky "
    # fi

    # xfce4-power-manager &

    # 通知工具
    if [[  -x "$( which dunst )" ]]; then
        echo "[INFO] 有安装 dunst "
        # dunst -conf ~/.dwm/scripts/config/dunst.conf &
        restart_app "dunst"  "dunst -conf "$HOME"/.dwm/scripts/config/dunst.conf "
    else
        echo "[INFO] 无 dunst "
    fi
    # 
    # lemonade server &

    # ~/.dwm/scripts/app-starter.sh easyeffects &
    
    # 网络
    # nm-applet >> /dev/null 2>&1 &
    if [[ -x "$( which nm-applet )" ]]; then
        echo "[INFO] 有安装 nm-applet"
        run_no_restart_app "nm-applet"
    else
        echo "[INFO] 无 nm-applet "
    fi

    # 截图工具
    # flameshot &
    if [[ -x "$( which flameshot)" ]]; then
        echo "[INFO] 有安装 flameshot"
        run_no_restart_app "flameshot"
    else
        echo "[INFO] 无 flameshot "
    fi

    # 坚果云
    # 判断是否运行eww如果有则打开eww的controllpanel面板
    if [ -x "$( which nutstore )" ]; then
        echo "[INFO] 有安装 nutstore"
        # nutstore >> /dev/null 2>&1 &
        run_no_restart_app "nutstore"
    elif [ -x "$HOME/.nutstore/dist/bin/nutstore-pydaemon.py" ]; then
        echo "[INFO]  有 $HOME/.nutstore/dist/bin/nutstore-pydaemon.py"
        # fedora的坚果云启动命令所在入口
        # exec ~/.nutstore/dist/bin/nutstore-pydaemon.py
        # exec ~/.nutstore/dist/bin/nutstore-pydaemon.py >> /dev/null 2>&1 &
        pids=$(ps -ef | grep "nutstore" | grep -v grep  | awk '{print $2}')
        if [[ $pids ]]; then
            echo "[INFO] nutstore is runing "
        else 
            echo "[INFO] start nutstore "
            exec ~/.nutstore/dist/bin/nutstore-pydaemon.py >> /dev/null 2>&1 &
        fi
        # run_no_restart_app "nutstore" "$HOME"/.nutstore/dist/bin/nutstore-pydaemon.py"
    else
        echo "[INFO] 无 nutstore "
    fi
    # utools
    if [[ -x "$( which utools )" ]]; then
        echo "[INFO] 有 utool"
        # utools &
        run_no_restart_app "utool"
    else
        echo "[INFO] 无 utool"
    fi

    ## 锁屏
    # 10分钟调用slock锁屏
    # xautolock -time 10 -locker slock  &
    # xautolock -time 10 -locker ~/.dwm/.scripts/xslock.sh  &
    # 10分钟调用i3lock锁屏
    # xautolock -time 10 -locker ~/.dwm/scripts/i3lock-get-screen-image-fancy &
    # 10分钟调用锁屏:i3lock
    if [[ -x "$( which xautolock )" ]]; then
        echo "[INFO] 有安装 xautolock"
        # xautolock -time 10 -locker  ~/.dwm/scripts/i3lock-get-screen-image-fancy.sh &
        # xautolock -time 10 -locker  "~/.dwm/scripts/app-starter.sh blurlock" &
        restart_app "xautolock" "xautolock -time 10 -locker  "$HOME"/.dwm/scripts/i3lock-get-screen-image-fancy.sh"
        # restart_app "xautolock" "xautolock -time 10 -locker  '$HOME/.dwm/scripts/app-starter.sh blurlock'"
    else
        echo "[INFO] 无 xautolock"
    fi
    # 
}

# every1s() {
#     # 延迟几秒
#     [ $1 ] && sleep $1
#     while true
#     do
#         # ~/.dwm/scripts/set-screen.sh check &
#         # 设置dwmstatus状态栏内容
#         ~/.dwm/scripts/dwm-status.sh &
#         sleep 1
#     done
# }

settings 1 &
# 延迟几秒执行 需要守护的进程
daemons 3 &
# 延迟 3 秒 执行每秒执行一次的程序
# every1s 10 &

# 添加环境变量配置/etc/profile
    # ```
    # # # 输入法设置
    # export LC_ALL=zh_CN.UTF-8
    # export XIM=fcitx
    # export XIM_PROGRAM=fcitx
    # export GTK_IM_MODULE=fcitx
    # export QT_IM_MODULE=fcitx
    # export XMODIFIERS="@im=fcitx"
    #
    # # > LC_ALL=zh_CN.UTF-8 是全局，包括菜单栏的文字都是中文的，如果你还想用英文的菜单，可以用以下的代码。
    # # export LC_CTYPE=zh_CN.UTF-8
    # # export XIM=fcitx
    # # export XIM_PROGRAM=fcitx
    # # export GTK_IM_MODULE=fcitx
    # # export QT_IM_MODULE=fcitx
    # # export XMODIFIERS="@im=fcitx"
    # # eval `dbus-launch --sh-syntax --exit-with-session`
    # # exec fcitx &
    #
    # # other
    # # idea界面在Archlinux的dwm中不显示问题解决需要环境变量配置
    # export _JAVA_AWT_WM_NONREPARENTING=1
    # export AWT_TOOLKIT=MToolkit
    # 解决在窗口管理器发生的Java GUIs渲染问题
    # 冒充另一个窗口管理器i（冒充 LG3D）
    # 启动后执行wmname LG3D
    # ```

